package com.king.designpatterns.hybriddesignpattern.hybriddesignpattern.chainofcommandandresponsibility;

/**
 * Invoker类负责命令的分发
 *
 * @author king
 * @date 2021-06-11 15:38
 */
public class Invoker {

    /**
     * 执行命令
     * @param _commandStr
     * @return
     */
    public String exec(String _commandStr){
        // 返回值
        String result = "";
        // 解析命令
        CommandVO vo = new CommandVO(_commandStr);
        // 检查是否支持该命令
        if (CommandEnum.getNames().contains(vo.getCommandName())) {
            String className = CommandEnum.valueOf(vo.getCommandName()).getValue();
            try {
                Command command = (Command) Class.forName(className).newInstance();
                result = command.execute(vo);
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            result = "无法执行命令，请检查命令格式";
        }

        return result;
    }
}
